package th.co.exat.model;

import java.util.ArrayList;
import java.util.List;
import th.co.exat.model.container.ErrorHandle;
import th.co.exat.model.container.UserContainer;
import th.co.exat.util.Encrypt;

public class ApplicationSecurity implements iSecurity {

    public ErrorHandle login(String userName, String userPassword) throws Exception {
        List<ErrorHandle> errors = new ArrayList<ErrorHandle>();
        ErrorHandle e = new ErrorHandle();
        UserContainer u = User.getByUserName(userName);
        userPassword = Encrypt.MD5(userPassword);

        if (userName.length() <= 0) {
            errors.add(new ErrorHandle());
        }
        if (userPassword.length() <= 0) {
            errors.add(new ErrorHandle());
        }

        if(!userPassword.equals(u.getUserPassword())) {
            errors.add(new ErrorHandle());
        }

        if (errors.size() == 0) {          
            e.setCode("SUCCESS");
            e.setMessage("SUCCESS");
        } else {
            e.setCode("E");
            e.setMessage("***ข้อมูลการ Login ไม่ถูกต้อง***");
        }
        return e;
    }

    public void logout() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}
